<script src="__STATIC_LIB__echarts/build/dist/echarts.js"></script>
<form class="layui-form seller-form"  action="" >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">表单名称：</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="title" placeholder="请输入表单名称关键字" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="form-search"><i class="iconfont icon-chaxun"></i>筛选</button>
            <button type="button" class="layui-btn layui-btn-sm add-form"><i class="layui-icon">&#xe608;</i> 添加</button>
        </div>
    </div>
</form>

<div class="table-body">
    <table id="formTable" lay-filter="formTable"></table>
</div>

<script>
    layui.use(['table','form','layer','laydate'],function(){
        var layer = layui.layer, table = layui.table,form = layui.form,date = layui.laydate;
        // 定义变量保存当前页面和当前页面数据条数
        var currPage; 
        var dataNum;

        //执行渲染
        table.render({
            elem: '#formTable', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-115',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            id:'formTable',
            url: "{:url('form/index')}",
            cols: [[ //标题栏
                {type:'checkbox'},
                {type:'numbers'},
                {field: 'name', title: '表单名称', width:200,align:'center'},
                {field: 'desc', title: '表单描述', width:300,align:'center'},
                {field: 'type', title: '类型', width:80,align:'center'},
                {field: 'is_login', title: '需要登录', width:100,align:'center'},
                {field: 'utime',sort: true, title: '更新时间' , width:160,align:'center'},
                {field: 'sort', sort: true, title: '排序', width:100,align:'center'},
                {title:'操作',align:'center', width:300, toolbar:'#formBar'}
            ]], //设置表头
            done: function(res, curr, count){
                // 给变量赋值 
                dataNum = res.data.length;
                currPage = curr;
            }
        });
        //search
        date.render({
            elem:'#utime'
        });
        form.on('submit(form-search)', function(data){
            layui.table.reload('formTable', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        $(document).on('click','.add-form',function(){
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage('{:url("Form/add")}', '添加表单'); 
            return false;
        });

        //监听工具条
        table.on('tool(formTable)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
           
            if(layEvent === 'del'){ //删除
                layer.confirm('真的要删除么',{icon: 3}, function(index){
                    JsGet("{:url('form/del')}?id=" + data.id, function(res){
                        //删除逻辑中判断只有一条数据时应该刷新前一页信息
                        layer.msg(res.msg);
                        if(res.status){
                            if(dataNum == 1){
                                currPage = currPage - 1;
                            }

                            table.reload('formTable', {
                                page: {
                                    curr: currPage
                                }
                            });
                            return false;
                        }
                    })
                });
            } else if(layEvent === 'edit'){ //编辑
                var editUrl = "{:url('Form/edit')}?id="+data.id;
                var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
                topLayui.index.openTabsPage(editUrl, '编辑表单'); 
                return false; 
            }else if(layEvent == 'generate'){
                window.box = layer.open({
                    type: 2,
                    content: "{:url('Form/generate')}?id="+data.id,
                    area: ['380px', '420px'],
                    title:'二维码',
                    btn:['下载','关闭'],
                    btnAlign:'c',
                    yes: function(index, layero){
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.downloadQrcode();
                    },
                    cancel: function(){
                        layer.closeAll();
                    }
                });
            }else if(layEvent == 'report'){
                var url = "{:url('Form/report')}?id="+data.id;
                var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
                topLayui.index.openTabsPage(url, '报表'); 
                return false; 
            }
        });

    })


</script>
<script type="text/html" id="formBar">
    <a class="layui-btn layui-btn-xs" lay-event="generate">小程序码</a>
    <a class="layui-btn layui-btn-xs" lay-event="report">报表</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
